package com.minxi.web.controller;

import com.minxi.web.javabean.UserInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "UpdateServlet",urlPatterns = "/update")
public class UpdateServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        //1.获取请求数据
        String id = req.getParameter("id");
        String userName = req.getParameter("userName");
        String userPhone = req.getParameter("userPhone");
        String userSex = req.getParameter("userSex");
        //2.JDBC
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        //2.请求数据库 JDBC
        try {
            //1.注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获取连接
            String url = "jdbc:mysql://localhost:3306/student?serverTimezone=Asia/Shanghai";
            String user = "root";
            String pwd = "123456";
            connection = DriverManager.getConnection(url, user, pwd);
            //3.编辑SQL语句 (预编译的执行对象)
            String sql = "update user_info set user_name=?,user_phone=?,user_sex=? where id = ?;";
            //得到执行对象
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,userName);
            preparedStatement.setString(2,userPhone);
            preparedStatement.setString(3,userSex);
            preparedStatement.setInt(4,Integer.parseInt(id));
            int rs = preparedStatement.executeUpdate();
            if(rs > 0){
                //查询新数据
                getUserInfo(req,resp);
                //跳转
                resp.sendRedirect("jsp/home.jsp");
            }else {
                resp.sendRedirect("jsp/update.jsp");
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(null != preparedStatement){
                    preparedStatement.close();
                }
                if(null != connection){
                    connection.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    public void getUserInfo(HttpServletRequest req, HttpServletResponse resp){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        //集合泛型
        List<UserInfo> list = new ArrayList<>();
        //2.请求数据库 JDBC
        try {
            //1.注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获取连接
            String url = "jdbc:mysql://localhost:3306/student?serverTimezone=Asia/Shanghai";
            String user = "root";
            String pwd = "123456";
            connection = DriverManager.getConnection(url, user, pwd);
            //3.编辑SQL语句 (预编译的执行对象)
            String sql = "select id,user_name,user_age,user_sex,user_phone from user_info;";
            //得到执行对象
            preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            //数据处理
            while (resultSet.next()){
                int id = resultSet.getInt("id");
                String userName = resultSet.getString("user_name");
                int userAge = resultSet.getInt("user_age");
                int userSex = resultSet.getInt("user_sex");
                String userPhone = resultSet.getString("user_phone");

                //怎么来封装这些字段数据？ 使用字符串比较麻烦，不好在页面展示
                UserInfo userInfo = new UserInfo();
                userInfo.setId(Long.parseLong(String.valueOf(id)));
                userInfo.setUserName(userName);
                userInfo.setUserAge(userAge);
                userInfo.setUserSex(userSex);
                userInfo.setUserPhone(userPhone);
                list.add(userInfo);
            }
            //如何将list放到页面上使用 HttpServletRequest Session(会话域)
            HttpSession session1 = req.getSession();
            //K V结构
            session1.setAttribute("userList",list);
            //跳转到指定的页面
            resp.sendRedirect("jsp/home.jsp");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(null != preparedStatement){
                    preparedStatement.close();
                }
                if(null != connection){
                    connection.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
